package geppetto.phraseScorer;

import java.util.HashMap;

import geppetto.cat.alignments.AlignmentsSet;
import geppetto.main.adaptation.AdaptationInfo;

public class AdaptationScorer extends AbstractPhraseScorer{

	public static String OTHER_TYPE_KEY = "OTHER";
	
	AlignmentsSet alignments;
	AdaptationInfo adaptationInfo;
	HashMap<String, Double> type_weights;
	public AdaptationScorer(AlignmentsSet alignments, AdaptationInfo adaptationInfo, HashMap<String, Double> type_weights) {
		this.alignments = alignments;
		this.adaptationInfo = adaptationInfo;
		this.type_weights = type_weights;
		normalizeWeights();
	}
	
	public double getPhraseScore(int sentenceNumber,byte sentenceSource,
			int[] foreingSentence, int[] sourceSentence,
			int sourceStartPos, int sourceEndPos,
			int foreignStartPos, int foreignEndPos){
		String[] typeArray = adaptationInfo.getSentenceTokenTypes(sentenceNumber);
		double n = sourceEndPos - sourceStartPos +1;
		double total = 0;
		for(int i = sourceStartPos; i <= sourceEndPos; i++){
			if(type_weights.containsKey(typeArray[i])){
				total += type_weights.get(typeArray[i]);
			}
			else{
				total += type_weights.get(OTHER_TYPE_KEY);
			}
		}		
		return total / n;
	}
	
	public void normalizeWeights(){
		double max = 0;
		for(String key : type_weights.keySet()){
			if(max < type_weights.get(key)){
				max = type_weights.get(key);
			}
		}
		for(String key : type_weights.keySet()){
			type_weights.put(key, type_weights.get(key) / max);
		}
	}
	
	
//	public abstract double getNullPhraseScore(int sentenceNumber, byte sentenceSource, 
//			int[] foreingSentence, int[] sourceSentence,
//			int foreignStartPos, int foreignEndPos);
	
}
